package boundary;

import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import dataview.ValutazioneRicetta;
import exceptions.DBException;
import store.TutteRicette;
import store.TuttiMenu;
import store.ValutazioniMenuAttive;
import store.ValutazioniRicettaAttive;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;

public class AttivareValutazioni extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JCheckBox vegetarianaRB = null;
	private JCheckBox veganaRB = null;
	private JCheckBox calorieRB = null;
	private JPanel radioButtonPanel = null;
	private JPanel buttonBar = null;
	private JButton confermaB = null;
	private Home home;
	private boolean ricetta;
	
	public AttivareValutazioni(Home home, boolean ricetta) {
		super();
		this.ricetta = ricetta;
		this.home = home;
		initialize();
		setChecks();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		gridBagConstraints7.gridx = 0;
		gridBagConstraints7.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints7.gridy = 1;
		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		gridBagConstraints6.gridx = 0;
		gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints6.gridy = 2;
		this.setLayout(new GridBagLayout());
		this.setSize(395, 213);
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		jLabel = new JLabel();
		jLabel.setText("Seleziona il tipo di valutazione da attivare");
		jLabel.setFont(new Font("Dialog", Font.BOLD, 20));
		this.add(getButtonBar(), gridBagConstraints6);
		this.add(jLabel, gridBagConstraints);
		this.add(getRadioButtonPanel(), gridBagConstraints7);
	}
	
	/**
	 * This method initializes vegetarianaRB
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JCheckBox getVegetarianaRB() {
		if (vegetarianaRB == null) {
			vegetarianaRB = new JCheckBox();
			vegetarianaRB.setText("vegetariana");
			vegetarianaRB.setFont(new Font("Dialog", Font.PLAIN, 12));
		}
		return vegetarianaRB;
	}
	
	/**
	 * This method initializes veganaRB
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JCheckBox getVeganaRB() {
		if (veganaRB == null) {
			veganaRB = new JCheckBox();
			veganaRB.setText("vegana");
			veganaRB.setFont(new Font("Dialog", Font.PLAIN, 12));
		}
		return veganaRB;
	}
	
	/**
	 * This method initializes calorieRB
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JCheckBox getCalorieRB() {
		if (calorieRB == null) {
			calorieRB = new JCheckBox();
			calorieRB.setText("calorie per persona");
			calorieRB.setFont(new Font("Dialog", Font.PLAIN, 12));
		}
		return calorieRB;
	}
	
	/**
	 * This method initializes radioButtonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getRadioButtonPanel() {
		if (radioButtonPanel == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints3.gridy = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.gridy = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = -1;
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.gridy = -1;
			radioButtonPanel = new JPanel();
			radioButtonPanel.setLayout(new GridBagLayout());
			radioButtonPanel.add(getVegetarianaRB(), gridBagConstraints1);
			radioButtonPanel.add(getVeganaRB(), gridBagConstraints2);
			radioButtonPanel.add(getCalorieRB(), gridBagConstraints3);
		}
		return radioButtonPanel;
	}
	
	/**
	 * This method initializes buttonBar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonBar() {
		if (buttonBar == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.anchor = GridBagConstraints.CENTER;
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			buttonBar = new JPanel();
			buttonBar.setLayout(new GridBagLayout());
			buttonBar.add(getConfermaB(), gridBagConstraints5);
		}
		return buttonBar;
	}
	
	/**
	 * This method initializes confermaB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getConfermaB() {
		if (confermaB == null) {
			confermaB = new JButton();
			confermaB.setText("Conferma");
			confermaB.setFont(new Font("Dialog", Font.BOLD, 12));
			confermaB.setIcon(new ImageIcon(getClass().getResource("/icone/accept-icon.png")));
			confermaB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (JOptionPane.showConfirmDialog(getRadioButtonPanel(), "Confermare la variazione delle valutazioni de" + (ricetta ? "lle ricette" : "i menu") + "?", "Conferma Modifica", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION) {
						try {
							if (ricetta) {
								TutteRicette.getTutteRicette().modificaValutazioni(new ValutazioniRicettaAttive(getVeganaRB().isSelected(), getVegetarianaRB().isSelected(), getCalorieRB().isSelected()));
								home.passaRicette(null);
								home.lr.getCompletaRB().setSelected(true);
							} else {
								TuttiMenu.getTuttiMenu().modificaValutazioniAttive(new ValutazioniMenuAttive(getVeganaRB().isSelected(), getVegetarianaRB().isSelected(), getCalorieRB().isSelected()));
								home.passaMenu(null);
								home.lm.getCompletaRB().setSelected(true);
							}
						} catch (DBException e1) {
							System.out.println(e1);
							JOptionPane.showMessageDialog(getRadioButtonPanel(), "Errore nel settare le valutazioni attive", "Errore", JOptionPane.ERROR_MESSAGE);
							return;
						}
						JOptionPane.showMessageDialog(getRadioButtonPanel(), "Modifica valutazioni effettuata", "Operazione Riuscita", JOptionPane.INFORMATION_MESSAGE);
					} else {

					}
				}
			});
		}
		return confermaB;
	}
	
	private void setChecks() {
		try {
			if (ricetta) {
				ValutazioniRicettaAttive v = TutteRicette.getTutteRicette().valutazioniAttive();
				getVeganaRB().setSelected(v.eAttiva(ValutazioneRicetta.vegana));
				getVegetarianaRB().setSelected(v.eAttiva(ValutazioneRicetta.vegetariana));
				getCalorieRB().setSelected(v.eAttiva(ValutazioneRicetta.calorie));
			} else {
				ValutazioniMenuAttive v = TuttiMenu.getTuttiMenu().valutazioniAttive();
				getVeganaRB().setSelected(v.eAttiva(ValutazioneRicetta.vegana));
				getVegetarianaRB().setSelected(v.eAttiva(ValutazioneRicetta.vegetariana));
				getCalorieRB().setSelected(v.eAttiva(ValutazioneRicetta.calorie));
			}
		} catch (Exception e) {
			System.err.println(e);
		}
	}
	
} //  @jve:decl-index=0:visual-constraint="17,8"
